﻿using ArtGalleryApp.DataModels;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace ArtGalleryApp.Data.Migrations
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool seedRoles = true, bool seedUsers = true, bool seedArt = true)
        {
            var adminUser = new ApplicationUser()
            {
                UserName = "iamAdmin",
                Email = "Admin@adminMail.com"
            };
            var generalUser = new ApplicationUser()
            {
                UserName = "iamUser",
                Email = "User@generalMail.com"
            };

            if (seedRoles)
            {
                RoleManager<IdentityRole> roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
                if (!roleManager.RoleExists("Admin"))
                {
                    roleManager.Create(new IdentityRole("Admin"));
                }
                if (!roleManager.RoleExists("GeneralUser"))
                {
                    roleManager.Create(new IdentityRole("GeneralUser"));
                }
                db.SaveChanges();
            }

            if (seedUsers)
            {
                UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
                if (userManager.FindByName("iamAdmin")==null){
                    var result = userManager.Create(adminUser, "adminpassword");
                    db.SaveChanges();
                    if (result.Succeeded)
                    {
                        //userManager.AddToRole("iamAdmin", "Admin");
                        userManager.AddToRole(userManager.FindByName("iamAdmin").Id, "Admin");
                    }
                }
                if(userManager.FindByName("iamUser")==null){
                    var result2 = userManager.Create(generalUser, "password");
                    db.SaveChanges();
                    if(result2.Succeeded){
                        //userManager.AddToRole("iamUser","GeneralUser");
                        userManager.AddToRole(userManager.FindByName("iamUser").Id, "GeneralUser");
                    }
                }
                db.SaveChanges();
            }

            if (seedArt)
            {
                adminUser = db.Users.Where(x => x.UserName == "iamAdmin").FirstOrDefault();
                db.ArtPieces.AddOrUpdate(
                    x => x.Name,
                    new ArtPiece() { Name = "Nocturnal flight", Artist = "Kakta Adams", Image = "http://www.artpiecegallery.com.au/images/artwork/Nocturnal%20flight.jpeg", ApplicationUserId = adminUser.Id },
                    new ArtPiece() { Name = "Orchestral Par Terre", Artist = "Colleen daRosa", Image = "http://www.artpiecegallery.com.au/images/artwork/colleen_darosa_Orchestral%20Par%20Terre%20linocut%20on%20Hahnemuhle%20(cut)65cmx%2085cm%20framed%20in%20perpex%20box%20email.jpg", ApplicationUserId = adminUser.Id },
                    new ArtPiece() { Name = "Town I", Artist = "Martin Doley", Image = "http://www.artpiecegallery.com.au/images/artwork/Martin%20Doley%20Town%20I%20acrylic%20on%20canvas%20100%20cm%20x%201200%20cm%20.jpg", ApplicationUserId = adminUser.Id },
                    new ArtPiece() { Name = "Bright Place", Artist = "Amanda van Gils", Image = "http://www.artpiecegallery.com.au/images/artwork/van-Gils-Amanda_Bright%20Place_3_2013_oil-on-linen_91x122cm.jpg", ApplicationUserId = adminUser.Id },
                    new ArtPiece() { Name = "Almost There", Artist = "Amanda van Gils", Image = "http://www.artpiecegallery.com.au/images/artwork/van-Gils-Amanda_Almost-there_2012_oilonlinen_91x122cm.jpg", ApplicationUserId = adminUser.Id },
                    new ArtPiece() { Name = "Night Began To Fall", Artist = "Amanda van Gils", Image = "http://www.artpiecegallery.com.au/images/artwork/Amanda%20van%20Gils%20Night%20Began%20to%20Fall.jpg", ApplicationUserId = adminUser.Id },
                    new ArtPiece() { Name="Joshua", Artist="Joshua", Image = "Joshua", ApplicationUserId=adminUser.Id}
                    );
                db.SaveChanges();
            }

        }
    }
}
